package cn.valorin.event;

import static cn.valorin.configuration.languagefile.MessageSender.sm;

import java.util.ArrayList;
import java.util.List;

import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;

import cn.valorin.gui.MyInventoryHolder;
import cn.valorin.item.Bean;
import cn.valorin.item.Gui0;
import cn.valorin.item.RawBean;
import cn.valorin.item.RawRice;
import cn.valorin.item.Rice;
import cn.valorin.util.CheckLore;

public class Gui0Event implements Listener {
	@SuppressWarnings("deprecation")
	@EventHandler
	public void click(InventoryClickEvent e) {
		Inventory inv = e.getInventory();
		if (inv == null) { return; }
		if (inv.getHolder() == null) { return; }
		if (!(inv.getHolder() instanceof MyInventoryHolder)) { return; }
		if (((MyInventoryHolder)inv.getHolder()).getType() != 0) { return; }
		int slot = e.getRawSlot();
		if (e.getClick().equals(ClickType.DOUBLE_CLICK)) {
			if (slot > 53 && slot < 90) {
			    e.setCancelled(true);
			    return;
			}
		}
		int[] availableSlots = {20,21,22,23,24,29,30,31,32,33};
		if (slot >=0 && slot <= 53) {
		    List<Integer> availableSlotsList = new ArrayList<Integer>();
		    for (int s : availableSlots) {
		    	availableSlotsList.add(s);
		    }
			if (!availableSlotsList.contains(slot)) { 
				e.setCancelled(true);
			}
		}
		
		Player opener = (Player) e.getWhoClicked();
		if (slot == 49) {
			if (inv.getItem(49).equals(Gui0.get1(opener))) {
				return;
			}
			if (e.getCursor().getType().equals(Material.WATER_BUCKET)) {
				e.setCursor(new ItemStack(Material.BUCKET));
				inv.setItem(49, Gui0.get1(opener));
			}
		}
		
		if (slot == 43) {
			if (inv.getItem(49).equals(Gui0.get0(opener))) {
				sm("&c请加入水",opener);
				return;
			}
			for (int availableSlot : availableSlots) {
				if (inv.getItem(availableSlot) !=null &&
					(!CheckLore.check(inv.getItem(availableSlot), RawBean.get(opener)) &&
					 !CheckLore.check(inv.getItem(availableSlot), RawRice.get(opener)))) {
					sm("&c请取走入料槽中除了生豆子和糯米以外的物品",opener);
					return;
				}
			}
			
			inv.setItem(49, Gui0.get0(opener));
			for (int availableSlot : availableSlots) {
				if (CheckLore.check(inv.getItem(availableSlot), RawBean.get(opener))) {
					ItemStack temp = Bean.get(opener);
					temp.setAmount(inv.getItem(availableSlot).getAmount());
					inv.setItem(availableSlot, temp);
				}
				if (CheckLore.check(inv.getItem(availableSlot), RawRice.get(opener))) {
					ItemStack temp = Rice.get(opener);
					temp.setAmount(inv.getItem(availableSlot).getAmount());
					inv.setItem(availableSlot, temp);
				}
			}
			sm("&a工序完成",opener);
		}
	}
	@EventHandler
	public void close(InventoryCloseEvent e) {
		Inventory inv = e.getInventory();
		if (inv == null) { return; }
		if (inv.getHolder() == null) { return; }
		if (!(inv.getHolder() instanceof MyInventoryHolder)) { return; }
		if (((MyInventoryHolder)inv.getHolder()).getType() != 0) { return; }
		Player player = (Player) e.getPlayer();
		int[] slots = {20,21,22,23,24,29,30,31,32,33};
		Location loc = player.getLocation();
		for (int slot : slots) {
			if (inv.getItem(slot) != null) {
			    player.getWorld().dropItem(loc, inv.getItem(slot));
			}
		}
	}
}
